package hasthi.tools;

import hasthi.common.HasthiException;
import hasthi.common.HasthiException.FaultCode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class CmdArgsParser {
    private Properties properties;
    private int minArgCount;
    private int maxArgCount;

    private List<String> parameters;

    public CmdArgsParser(int minArgCount, int maxArgCount) {
        super();
        this.minArgCount = minArgCount;
        this.maxArgCount = maxArgCount;
        parameters = new ArrayList<String>(maxArgCount);
    }
    
    
    /**
     * Any argument does not start with - treated as a argument
     * Any argument that matches -D<name>=<value> treat as a option, and send the base context iitialization
     * Argument that matches -Dconfig=<filename> is used to load the initial properties
     * Any argument that matches -D<name> is considered as that option is enabled
     * @param args
     * @throws HasthiException
     */

    public void parse(String[] args) throws HasthiException {
        try {
            for (String arg : args) {
                if (arg.startsWith("-")) {
                    arg = arg.replaceAll("-", "");
                    if (arg.contains("=")) {
                        String[] prop = arg.split("=");
                        if (properties == null) {
                            properties = new Properties();
                        }
                        if (prop.length == 2) {
                            throw new HasthiException("Misformatted property " + arg,
                                    FaultCode.ConfigError);
                        }
                        properties.setProperty(prop[0], prop[1]);
                        if(prop[0].startsWith("sys:")){
                            System.setProperty(prop[0].replace("sys:", ""), prop[1]);
                        }else if(prop[0].equals("config")){
                            FileInputStream in = new FileInputStream(prop[1]);
                            properties.load(in);
                            in.close();
                        }
                    } else {
                        properties.setProperty(arg.replace("-", ""), Boolean.TRUE.toString());
                    }
                } else {
                    parameters.add(arg);
                }
            }
            if (parameters.size() < minArgCount && parameters.size() > maxArgCount) {
                throw new HasthiException("Must have " + minArgCount + " and max " + maxArgCount
                        + "arguments", FaultCode.ConfigError);
            }
        } catch (FileNotFoundException e) {
            throw new HasthiException(e,FaultCode.ConfigError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.ConfigError);
        }
    }

    public Properties getProperties() {
        return properties;
    }

    public String getParameter(int i) {
        return parameters.get(i);
    }

    public String getOption(String name) {
        return properties.getProperty(name);
    }

    public boolean isOptionENabled(String name) {
        return properties.getProperty(name) != null;
    }
    
    public String propertiesAsString(){
        if(properties != null){
            StringBuffer buffer = new StringBuffer();
            for(Object key:properties.keySet()){
                buffer.append(key).append("=").append(properties.get(key)).append(" ");
            }
            return buffer.toString();
        }
        return "";
    }
}
